package net.lab1024.sa.admin.module.business.exam.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.exam.dao.ExamromDao;
import net.lab1024.sa.admin.module.business.exam.dao.ExamromaDao;
import net.lab1024.sa.admin.module.business.exam.dao.ExamrombDao;
import net.lab1024.sa.admin.module.business.exam.dao.PaperDao;
import net.lab1024.sa.admin.module.business.exam.domain.entity.*;
import net.lab1024.sa.admin.module.business.exam.domain.form.add.ExamromAddForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.ExamromQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.ExamromaQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.update.ExamromUpdateForm;
import net.lab1024.sa.admin.module.business.exam.domain.vo.ExamromStaticVo;
import net.lab1024.sa.admin.module.business.exam.domain.vo.ExamromVO;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程分类 Service
 *
 * @Author AKITA
 * @Date 2025-06-06 11:33:42
 * @Copyright AKITA
 */

@Service
public class ExamromService {

    @Resource
    private ExamromDao examromDao;
    @Autowired
    private ExamromaDao examromaDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private PaperService paperService;
    @Autowired
    private ExamrombDao examrombDao;
    @Autowired
    private PaperDao paperDao;

    /**
     * 分页查询
     */
    public PageResult<ExamromVO> queryPage(ExamromQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ExamromVO> list = examromDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<Long> add(ExamromAddForm addForm) {
        ExamromEntity examromEntity = SmartBeanUtil.copy(addForm, ExamromEntity.class);
        examromDao.insert(examromEntity);
        return ResponseDTO.ok(examromEntity.getDocEntry());
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(ExamromUpdateForm updateForm) {
        ExamromEntity examromEntity = SmartBeanUtil.copy(updateForm, ExamromEntity.class);

        if (examromDao.updateById(examromEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }


        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        examromDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        examromDao.deleteById(docEntry);
        examromaDao.deleteById(docEntry);
        examrombDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<ExamromEntity> info(Long docEntry) {
        ExamromEntity examromEntity = examromDao.selectById(docEntry);
        examromEntity.setLista(examromaDao.selectList(new LambdaQueryWrapper<ExamromaEntity>().eq(ExamromaEntity::getDocEntry, docEntry)));
        examromEntity.setListb(examrombDao.selectList(new LambdaQueryWrapper<ExamrombEntity>().eq(ExamrombEntity::getDocEntry, docEntry)));
        return ResponseDTO.ok(examromEntity);
    }

    @Transactional
    public ResponseDTO<ExamromEntity> openAll(Long docEntry) {
        List<EmployeeEntity> employeeEntities = employeeDao.selectList(null);


        ExamromEntity examromEntity = examromDao.selectById(docEntry);
        ResponseDTO<PaperEntity> info = paperService.info(examromEntity.getPaperId());
        int lineNum = 0;
        int lineNuma = 0;
        for (EmployeeEntity employeeEntity : employeeEntities) {
            ExamromaEntity examromaEntity = new ExamromaEntity();
            examromaEntity.setDocEntry(docEntry);
            examromaEntity.setLineNum(++lineNum);
            examromaEntity.setLineStatus("O");
            examromaEntity.setUserId(employeeEntity.getEmployeeId());
            examromaEntity.setUserName(employeeEntity.getLoginName());
            examromaEntity.setVersion(1);
            examromaDao.insert(examromaEntity);
            for (PaperaEntity paperaEntity : info.getData().getLista()) {
                ExamrombEntity examrombEntity = new ExamrombEntity();
                examrombEntity.setDocEntry(docEntry);
                examrombEntity.setLineNum(++lineNuma);
                examrombEntity.setLineStatus("O");
                examrombEntity.setUserId(employeeEntity.getEmployeeId());
                examrombEntity.setUserName(employeeEntity.getLoginName());
                examrombEntity.setQuestion(paperaEntity.getQuestion());
                examrombEntity.setBaseScore(paperaEntity.getScore());
                examrombEntity.setQuesTypeId(paperaEntity.getQuesTypeId());
                examrombEntity.setQuesTypeName(paperaEntity.getQuesTypeName());
                examrombEntity.setBaseEntry(paperaEntity.getBaseEntry());
                examrombDao.insert(examrombEntity);
            }
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<List<ExamromEntity>> myList(ExamromaQueryForm lineStatus) {
        Long requestUserId = SmartRequestUtil.getRequestUserId();
        List<ExamromEntity> entityList = examromDao.myList(requestUserId, lineStatus.getLineStatus());

        return ResponseDTO.ok(entityList);
    }

    public ResponseDTO<ExamromEntity> myInfo(Long docEntry) {
        Long requestUserId = SmartRequestUtil.getRequestUserId();
        ExamromEntity examromEntity = examromDao.myInfo(requestUserId, docEntry);
        return ResponseDTO.ok(examromEntity);
    }

    public ResponseDTO<List<ExamromStaticVo>> statis(ExamromQueryForm queryForm) {
        return ResponseDTO.ok(examromDao.statis(queryForm));
    }
}
